class Solution {
    public static String truncateSentence(String s, int k) {
        int i = 0;
        while (k > 0 && i <= s.length()) {
            char ch = s.charAt(i);
            if (ch == ' ') {
                k--;
            }
            i++;
        }
        return k == 0 ? s.substring(0, i - 1) : s;
    }

    public int removeDuplicates(int[] nums) {
        int former = 0;
        int len = 1;
        for (int i = 1; i < nums.length; i++) {
            if (nums[former] != nums[i]) {
                nums[++former] = nums[i];
                len++;
            }
        }
        return len;
    }

    public static void main(String[] args) {
        System.out.println(truncateSentence("chopper is not a tanuki", 5));
    }
}